package com.borya.dao.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.borya.framework.annotation.Repository;
import com.borya.framework.core.ServerRedisSession;
import com.borya.framework.core.ServerRedisSessionFactory;
abstract class BaseRedisImpl {

	protected Logger log = Logger.getLogger(getClass());
	
	@Repository("/conf/redis.properties")
	private ServerRedisSessionFactory sessionFactory;
		
	public ServerRedisSessionFactory getSessionFactory() {		
		return sessionFactory;
	}

	public String redis_get(String key){
		try (ServerRedisSession session = getSessionFactory().session()){
			return session.get(key);
		} catch (Exception e) {
			log.error(e.getMessage(),e);
			return null;
		}
	}
	
	public boolean redis_set(String key,String value){
		try (ServerRedisSession session = getSessionFactory().session()){
			return (session.set(key, value) != null);
		} catch (Exception e) {
			log.error(e.getMessage(),e);
			return false;
		}
	}

	public boolean redis_setNx(String key,String value,int seconds){
		try (ServerRedisSession session = getSessionFactory().session()){
			return ("OK".equals(session.set(key, value,"NX","EX",seconds)));
		} catch (Exception e) {
			log.error(e.getMessage(),e);
			return false;
		}
	}


	public boolean redis_setNx(String key,String value){
		try (ServerRedisSession session = getSessionFactory().session()){
			return (session.setnx(key, value) > 0);
		} catch (Exception e) {
			log.error(e.getMessage(),e);
			return false;
		}
	}
	
	//EXPIRE key seconds
	public boolean redis_expire(String key,int seconds){
		try (ServerRedisSession session = getSessionFactory().session()){
			return (session.expire(key, seconds) != null);
		} catch (Exception e) {
			log.error(e.getMessage(),e);
			return false;
		}
	}
	
	public boolean redis_hset(String key,String field,String value){
		try (ServerRedisSession session = getSessionFactory().session()){
			return  (session.hset(key, field, value) != null);
		} catch (Exception e) {
			log.error(e.getMessage(),e);
			return false;
		}
	}
	
	public String redis_hget(String key,String field){
		try (ServerRedisSession session = getSessionFactory().session()){
			return session.hget(key, field);
		} catch (Exception e) {
			log.error(e.getMessage(),e);
			return null;
		}
	}
	
	public boolean redis_del(String key){
		try (ServerRedisSession session = getSessionFactory().session()){
			return  (session.del(key) != null);
		} catch (Exception e) {
			log.error(e.getMessage(),e);
			return false;
		}
	}
	
	public Map<String,String> redis_hgetAll(String key){
		try (ServerRedisSession session = getSessionFactory().session()){
			return session.hgetAll(key);
		} catch (Exception e) {
			log.error(e.getMessage(),e);
			return null;
		}
	}
	
	public List<String> redis_brpop(final int timeout,final String key){
		try (ServerRedisSession session = getSessionFactory().session()){
			return session.brpop(timeout, key);
		} catch (Exception e) {
			log.error(e.getMessage(),e);
			return new ArrayList<String>();
		}
	}
	
	public Long redis_ttl(String key){
		try (ServerRedisSession session = getSessionFactory().session()){
			return  session.ttl(key);
		} catch (Exception e) {
			log.error(e.getMessage(),e);
			return null;
		}
	}
	public boolean redis_del(String key,String field){
		try (ServerRedisSession session = getSessionFactory().session()){
			return (session.hdel(key, field))!=null;
		} catch (Exception e) {
			log.error(e.getMessage(),e);
			return false;
		}
	}
	public String redis_lpop(String key){
		try (ServerRedisSession session = getSessionFactory().session()){
			return session.lpop(key);
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			return null;
		}
	}
	public Long redis_rpush(String key,String val){
		try (ServerRedisSession session = getSessionFactory().session()){
			return session.rpush(key, val);
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			return null;
		}
	}
	public String redis_hmset(final String key,Map<String,String> field){
		try (ServerRedisSession session = getSessionFactory().session()){
			return session.hmset(key, field);
		} catch (Exception e) {
			log.error(e.getMessage()+",operation failed :"+key+",field:"+field,e);
			return null;
		}
	}
}
